package com.slz3.business.module.goodsManagement.gooAttribute.steward;

import com.slz3.business.frame.functions.Add;
import com.slz3.business.frame.functions.Modify;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.goodsManagement.gooAttribute.domain.GooAttribute;
import com.slz3.business.module.goodsManagement.gooAttribute.domain.GooAttributeCondition;
import org.springframework.data.domain.Example;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.Optional;


public class GooAttributeResUnsafeValid {

    public static boolean add(ServerRequest req, GooAttribute gooAttribute) {
        Steward.constraintViolations(req, gooAttribute, Add.class);
        //判断相同类别中是否已经存在相同的名称了,如果存在返回该名称已经存在
        checkNameExistForAdd(req, gooAttribute);
        return RR.hasResErr(req);
    }

    public static boolean delete(ServerRequest req, GooAttributeCondition gooAttributeCondition) {
        if (VE.isEmpty(gooAttributeCondition.getUuidList())) {
            RR.resDetail(req).put("uuidList", "must not null");
        } else if (gooAttributeCondition.getUuidList().size() > 800) {
            RR.resDetail(req).put("uuidList", "uuidList max size is 800");
        }
        return RR.hasResErr(req);
    }

    static boolean modify(ServerRequest req, GooAttribute gooAttribute) {
        Steward.constraintViolations(req, gooAttribute, Modify.class);
        checkNameExistForModify(req, gooAttribute);
        return RR.hasResErr(req);
    }

    private static void checkNameExistForModify(ServerRequest req, GooAttribute gooAttribute) {
        GooAttribute attribute = new GooAttribute();
        attribute.setFkTypeLevel1(gooAttribute.getFkTypeLevel1());
        attribute.setFkTypeLevel2(gooAttribute.getFkTypeLevel2());
        attribute.setName(gooAttribute.getName());
        Example<GooAttribute> of = Example.of(attribute);
        Optional<GooAttribute> gooAttributeOptional = Repositorys.gooAttribute.findOne(of);
        if (gooAttributeOptional.isPresent()) {
            GooAttribute gooAttribute1 = gooAttributeOptional.get();
            if (!gooAttribute1.getUuid().equals(gooAttribute.getUuid())) {
                RR.resDetail(req).put("name", "name in the same type has existed");
            }
        }
    }

    private static void checkNameExistForAdd(ServerRequest req, GooAttribute gooAttribute) {
        GooAttribute attribute = new GooAttribute();
        attribute.setFkTypeLevel1(gooAttribute.getFkTypeLevel1());
        attribute.setFkTypeLevel2(gooAttribute.getFkTypeLevel2());
        attribute.setName(gooAttribute.getName());
        Example<GooAttribute> of = Example.of(attribute);
        boolean exists = Repositorys.gooAttribute.exists(of);
        if (exists) {
            RR.resDetail(req).put("name", "name in the same type has existed");
        }
    }

    public static boolean list(ServerRequest req) {
        return RR.hasResErr(req);
    }

    public static boolean info(ServerRequest req) {
        if (VE.isEmpty(req.queryParam("uuid").orElse(null))) {
            RR.resDetail(req).put("uuid", "must not null");
        }
        return RR.hasResErr(req);
    }

    public static boolean choice(ServerRequest req) {
        return RR.hasResErr(req);
    }

    public static boolean auditSubmit(ServerRequest req, GooAttributeCondition gooAttributeCondition) {
        if (VE.isEmpty(gooAttributeCondition.getUuidList())) {
            RR.resDetail(req).put("uuidList", "must not null");
        } else if (gooAttributeCondition.getUuidList().size() > 800) {
            RR.resDetail(req).put("uuidList", "uuidList max size is 800");
        }
        return RR.hasResErr(req);
    }

    public static boolean audit(ServerRequest req, GooAttributeCondition gooAttributeCondition) {
        if (VE.isEmpty(gooAttributeCondition.getUuidList())) {
            RR.resDetail(req).put("uuidList", "must not null");
        } else if (gooAttributeCondition.getUuidList().size() > 800) {
            RR.resDetail(req).put("uuidList", "uuidList max size is 800");
        }
        return RR.hasResErr(req);
    }

    public static boolean auditReject(ServerRequest req, GooAttributeCondition gooAttributeCondition) {
        if (VE.isEmpty(gooAttributeCondition.getUuidList())) {
            RR.resDetail(req).put("uuidList", "must not null");
        } else if (gooAttributeCondition.getUuidList().size() > 800) {
            RR.resDetail(req).put("uuidList", "uuidList max size is 800");
        }
        return RR.hasResErr(req);
    }

    public static boolean usingEnable(ServerRequest req, GooAttributeCondition gooAttributeCondition) {
        if (VE.isEmpty(gooAttributeCondition.getUuidList())) {
            RR.resDetail(req).put("uuidList", "must not null");
        } else if (gooAttributeCondition.getUuidList().size() > 800) {
            RR.resDetail(req).put("uuidList", "uuidList max size is 800");
        }
        return RR.hasResErr(req);
    }

    public static boolean usingDisable(ServerRequest req, GooAttributeCondition gooAttributeCondition) {
        if (VE.isEmpty(gooAttributeCondition.getUuidList())) {
            RR.resDetail(req).put("uuidList", "must not null");
        } else if (gooAttributeCondition.getUuidList().size() > 800) {
            RR.resDetail(req).put("uuidList", "uuidList max size is 800");
        }
        return RR.hasResErr(req);
    }
}
